package com.dzl.enterprise.service.impl;

import com.dzl.base.service.BaseServiceImpl;
import com.dzl.constant.CommonConstants;
import com.dzl.enterprise.dao.WelfareEnterpriseApiGoodsMapper;
import com.dzl.enterprise.pojo.WelfareEnterpriseApiGoods;
import com.dzl.enterprise.pojo.query.WelfareEnterpriseApiGoodsQuery;
import com.dzl.enterprise.pojo.vo.WelfareEnterpriseApiGoodsVo;
import com.dzl.enterprise.service.WelfareEnterpriseApiGoodsService;
import com.dzl.goods.pojo.vo.WelfareSkuGoodsVo;
import com.dzl.util.EmptyUtils;
import com.dzl.util.ImageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * api商品(welfare_enterprise_api_goods)表服务实现类
 * @author liuguanjun
 * @date 2021-11-23 16:42:23
 */
@Slf4j
@Service
public class WelfareEnterpriseApiGoodsServiceImpl extends BaseServiceImpl<WelfareEnterpriseApiGoods> implements WelfareEnterpriseApiGoodsService {

    @Autowired
    private WelfareEnterpriseApiGoodsMapper mapper;

    @Override
    public PageInfo<WelfareEnterpriseApiGoodsVo> getWelfareEnterpriseGoodsListPage(WelfareEnterpriseApiGoodsQuery query) {
        PageHelper.startPage(query);
        List<WelfareEnterpriseApiGoodsVo> list = mapper.selectEnterpriseGoodsList(query);
        list.forEach(goodsVo->{
            goodsVo.setGoodsImg(ImageUtils.setImageSrc(goodsVo.getGoodsImg()));
            if(goodsVo.getSkuStatus()== CommonConstants.YES && goodsVo.getGoodsStatus() == CommonConstants.YES){
                goodsVo.setGoodsStatus(CommonConstants.YES);
            }else{
                goodsVo.setGoodsStatus(CommonConstants.NO);
            }
        });
        PageInfo<WelfareEnterpriseApiGoodsVo> page = new PageInfo<WelfareEnterpriseApiGoodsVo>(list);
        return page;
    }

    @Override
    public Integer saveWelfareEnterpriseGoods(String skuIds,Integer enterpriseId) {
        List<WelfareEnterpriseApiGoods> goodsList = new ArrayList<>();
        //添加新選擇的商品
        List<WelfareSkuGoodsVo> list = mapper.selectGoodsAttrsBySkuIds(skuIds);
        list.forEach(x->{
            WelfareEnterpriseApiGoods welfareEnterpriseGoods = new WelfareEnterpriseApiGoods();
            welfareEnterpriseGoods.setGoodsId(x.getGoodsId());
            welfareEnterpriseGoods.setGoodsImg(x.getGoodsImg());
            if(EmptyUtils.isNotEmpty(x.getSkuPic())){
                welfareEnterpriseGoods.setGoodsImg(x.getSkuPic());
            }
            welfareEnterpriseGoods.setGoodsName(x.getGoodsName());
            StringBuffer goodsAttr = new StringBuffer();
            goodsAttr.append(x.getFirstDetailName());
            if(EmptyUtils.isNotEmpty(x.getSecondDetailName())){
                goodsAttr.append("/").append(x.getSecondDetailName());
            }
            if(EmptyUtils.isNotEmpty(x.getThirdDetailName())){
                goodsAttr.append("/").append(x.getThirdDetailName());
            }
            welfareEnterpriseGoods.setGoodsAttrs(goodsAttr.toString());
            welfareEnterpriseGoods.setSkuId(x.getId());
            welfareEnterpriseGoods.setStatus(CommonConstants.YES);
            welfareEnterpriseGoods.setSupplyPrice(x.getPrice());
            welfareEnterpriseGoods.setEnterpriseId(enterpriseId);
            welfareEnterpriseGoods.setCreateTime(new Date());
            welfareEnterpriseGoods.setUpdateTime(new Date());
            goodsList.add(welfareEnterpriseGoods);
        });
        if(EmptyUtils.isNotEmpty(goodsList)){
            return mapper.insertList(goodsList);
        }
        return 0;
    }

    @Override
    public Integer deleteWelfareEnterpriseGoods(String ids) {
        return mapper.deleteWelfareEnterpriseGoods(ids);
    }

    @Override
    public Integer updateWelfareEnterpriseGoodsSupplyPriceById(Integer id, BigDecimal supplyPrice) {
        WelfareEnterpriseApiGoods welfareEnterpriseGoods = new WelfareEnterpriseApiGoods();
        welfareEnterpriseGoods.setId(id);
        welfareEnterpriseGoods.setSupplyPrice(supplyPrice);
        welfareEnterpriseGoods.setUpdateTime(new Date());
        return mapper.updateByPrimaryKeySelective(welfareEnterpriseGoods);
    }

    @Override
    public Integer batchUpdateWelfareEnterpriseGoodsSupplyPrice(String ids, Double floatingRatio) {
        String[] ids_ = ids.split(",");
        for (String id:ids_) {
            mapper.updateWelfareEnterpriseGoodsSupplyPrice(floatingRatio, Integer.valueOf(id));
        }
        return ids_.length;
    }

    @Override
    public List<WelfareSkuGoodsVo> getGoodsAttrsByGoodsIdAndEnterpriseId(Integer goodsId, Integer enterpriseId) {
        List<WelfareSkuGoodsVo> list = mapper.selectGoodsAttrsByGoodsIdAndEnterpriseId(goodsId,enterpriseId);
        list.forEach(x->{
            if(x.getIsChecked()!=CommonConstants.NO){
                x.setIsChecked(CommonConstants.YES);
            }
            StringBuffer goodsAttr = new StringBuffer();
            goodsAttr.append(x.getFirstDetailName());
            if(EmptyUtils.isNotEmpty(x.getSecondDetailName())){
                goodsAttr.append("/").append(x.getSecondDetailName());
            }
            if(EmptyUtils.isNotEmpty(x.getThirdDetailName())){
                goodsAttr.append("/").append(x.getThirdDetailName());
            }
            x.setGoodsAttr(goodsAttr.toString());
        });
        return list;
    }
}
